﻿using System.Data.SqlClient;
using System;
using System.Diagnostics;
using System.Data;
using System.Collections;
using System.Data.Common;

namespace Persistencia
{
    public class GerenciadorConexao
    {
        private const byte MAXPOOLCONECTION = 5;
        private static DbProviderFactory factory = null;
        private static AssemblySettings asmCfg = null;
        private static IDictionary conexoes = new Hashtable(MAXPOOLCONECTION);

        private static void adicionaConexaoAoPool()
        {
            if (factory == null)
            {
                asmCfg = new AssemblySettings();
                factory = DbProviderFactories.GetFactory(asmCfg["DbProvider"]);
            }
            if (conexoes.Count <= MAXPOOLCONECTION - 1)
            {
                SqlConnection conn = new System.Data.SqlClient.SqlConnection();
                conn.ConnectionString = asmCfg["ConnectionString"];
                conexoes.Add(conexoes.Count, conn);
            }
            else
            {
                throw new Exception("O pool de conexão excedeu o limite máximo.");
            }
        }
        private static SqlConnection getConnectionPool()
        {
            SqlConnection sqlconn = null;
            if (conexoes.Count == 0) adicionaConexaoAoPool();

            foreach (SqlConnection conn in conexoes.Values)
            {
                if (conn != null && conn.State != ConnectionState.Open)
                {
                    sqlconn = conn;
                }
            }
            if (sqlconn == null)
            {
                adicionaConexaoAoPool();
                sqlconn = getConnectionPool();
            }
            return sqlconn;
        }

        public static SqlConnection getConexao()
        {
            SqlConnection sqlconn = null;
            try
            {
                sqlconn = getConnectionPool();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);

            }
            return sqlconn;
        }
        public static void closeConexao(SqlConnection conn)
        {
            try
            {
                conn.Close();
            }
            catch (NullReferenceException)
            {
            }
        }
        public static int getId(String tabela)
        {
            int id = 0;
            SqlConnection conn = getConnectionPool();
            conn.Open();
            try
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = "SELECT CAST(IDENT_CURRENT('" + tabela + "') AS INT) as maxid";
                command.Connection = conn;

                DbDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
                while (dr.Read())
                {
                    id = (int)dr["maxid"];
                }
                dr.Close();
                conn.Close();
                return id;
            }
            catch (SqlException e)
            {
                throw new Exception(e.Message);
            }
        }

    }
}
